﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace IHISCETeim.SendSMSSvr
{
    public class Common
    {
        private static string CurrentDirectory
        {
            get
            {
                string s = Environment.CommandLine;
                s = s.Replace("\"", "");
                s = System.IO.Path.GetDirectoryName(s);
                return s;
            }
        }

        private static string _LogFile = "";
        public static string LogFile
        {
            get
            {
                if (_LogFile == "")
                {
                    _LogFile = CurrentDirectory;
                    if (!_LogFile.EndsWith("\\"))
                        _LogFile += "\\";

                    _LogFile += "Log\\";
                    if (!Directory.Exists(_LogFile))
                        Directory.CreateDirectory(_LogFile);
                    //2015-01-20
                    IsOpen = false;
                }
                return _LogFile + DateTime.Now.ToString("yyyy-MM-dd") + "_Log.txt";
                //_LogFile += "Log.txt";
                //return _LogFile;
            }
        }

        private static bool IsOpen = false;
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="Message"></param>
        public static void WriteLog(string Message)
        {
            while(!IsOpen)
            {
                IsOpen = true;
                StreamWriter sw = null;
                using( sw = new StreamWriter(LogFile, true))
                { 
                    string Content;
                    Content = DateTime.Now.ToString() + Environment.NewLine;
                    Content += Message;
                    Content += Environment.NewLine;
                    sw.Write(Content);
                    sw.Close();
                    sw = null;
                }
                IsOpen = false;
                break;
            }
        }
    }
}
